import time
import httpx

cookie = ""
client = httpx.Client(
    headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'
        ,
        'Cookie': cookie
    },
    http1=True
)
url = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all'
url_ht ='https://app.bilibili.com/x/topic/web/details/top'
url_p = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/topic'

def dy_d(x):
    params = {
        'timezone_offset': '-480',
        'type': 'all',
        'platform': 'web',
        'page': 1,
        'features': ['itemOpusStyle', 'listOnlyfans', 'opusBigCover', 'onlyfansVote', 'decorationCard',
                     'onlyfansAssetsV2', 'forwardListHidden', 'ugcDelete', 'onlyfansQaCard', 'commentsNewVersion'],
        'web_location': '333.1365',
        'x-bili-device-req-json': '{"platform":"web","device":"pc"}',
        'x-bili-web-req-json': '{"spm_id":"333.1365"}',
        'offset': x
    }
    return client.get(url=url,params=params).json()
def ht_d(x):
    params_ht = {
        'topic_id': x,
        'source': 'Web'
    }
    params_p = {
        'topic_id': x,
        'sort_by': 0,
        'offset':'',
            'page_size': 20,
    'source': 'Web',
    'features': ['itemOpusStyle', 'listOnlyfans', 'opusBigCover', 'onlyfansVote', 'decorationCard']
    }
    ht = client.get(url=url_ht ,params=params_ht).json()
    client.get(url = url_p,params=params_p)
    return ht
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
dy = dy_d('')
x_len = len(dy['data']['items'])
x = 0
with open('ht_id', 'r') as f:
    lis = f.read()
    ht_id = lis.split(',')
print(ht_id)
name_lis = []
while x_len>0:
    time.sleep(2)
    offset = dy['data']['offset']
    if not ('orig' in dy['data']['items'][x]) and dy['data']['items'][x]['modules']['module_dynamic']['topic']!=None and not (str(dy['data']['items'][x]['modules']['module_dynamic']['topic']['id']) in ht_id):
        topic_id = dy['data']['items'][x]['modules']['module_dynamic']['topic']['id']
        print(topic_id,str(dy['data']['items'][x]['modules']['module_dynamic']['topic']['id']) in ht_id)
        ht = ht_d(topic_id)
        ht_id.append(str(topic_id))
        if 'functional_card' in ht['data'] and len(ht['data']['functional_card'])>0 and 'traffic_card' in ht['data']['functional_card']:
            if not(ht['data']['functional_card']['traffic_card']['name'] in name_lis):
                name_lis.append(ht['data']['functional_card']['traffic_card']['name']) 
                with open('ht_id', "a", encoding='utf-8') as file:
                    file.write(','+str(topic_id))
                with open('traffic_card.txt', "a", encoding='utf-8') as file:
                    file.write('发布时间：'+str(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(dy['data']['items'][x]['modules']['module_author']['pub_ts'])))+'   '+ht['data']['functional_card']['traffic_card']['name']+'            '+(ht['data']['functional_card']['traffic_card']['card_desc'] if 'card_desc' in ht['data']['functional_card']['traffic_card'] else '')+ '        '+ht['data']['functional_card']['traffic_card']['benefit_point']+'\n'+ht['data']['functional_card']['traffic_card']['jump_url']+'\n')
            print(dy['data']['items'][x]['modules']['module_author']['name'])
            print(ht['data']['functional_card']['traffic_card']['name'] + '~~~~~~~~~~~~~~~~~~~~~话题')
    print(dy['data']['items'][x]['modules']['module_author']['name'])
    x = x + 1
    x_len = x_len - 1
    if x_len==0 and len(dy['data']['offset'])>0:
        x = 0
        dy = dy_d(offset)
        x_len = len(dy['data']['items'])
